<?php
/*
Copyright (C) 2009 de Flotte Maxence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
?>
<?php
include_once './ZzDom/ZzDomType/domtype.php';
/**
 * DomType permetant d'enregistrer des string
 */
class DomTypeLongString extends DomType {
	
	public $quoted = true;
	
	/**
	 * Attribu une valeur au champ
	 * @return bool Retourne False si la value n'est pas correctement formulée, true dans le cas contraire
	 */
	public function setValue($val) {
		$this->value = stripslashes($val);
		return true;
	}

	/**
	 * Revoie le formulaire afin d'enregistrer/modifier le DomType
	 */
	function in() {
		return '<label for="'.$this->name.'">'.$this->description.' : </label><br /><textarea id="'.$this->name.'" name="'.$this->name.'" >'.html_entity_decode($this->value).'</textarea>';
	}
	/**
	 * Renvoie la valeur du DomType
	 */
	function out() {
		return $this->zzCode2html($this->value);
	}
	
	private function zzCode2html($text) {
		//tableau de remplacement
		$in = array();
		$out = array();
		$in[] = '_/ '; $out[] = '<i>';
		$in[] = ' /_'; $out[] = '</i>';
		$in[] = '_i '; $out[] = '<i>';
		$in[] = ' i_'; $out[] = '</i>';
		$in[] = '_u '; $out[] = '<u>';
		$in[] = ' u_'; $out[] = '</u>';
		$in[] = '__ '; $out[] = '<u>';
		$in[] = ' __'; $out[] = '</u>';
		$in[] = '_s '; $out[] = '<u>';
		$in[] = ' s_'; $out[] = '</u>';
		$in[] = '_g '; $out[] = '<b>';
		$in[] = ' g_'; $out[] = '</b>';
		$in[] = '_b '; $out[] = '<b>';
		$in[] = ' b_'; $out[] = '</b>';
		$in[] = '_! '; $out[] = '<em>';
		$in[] = ' !_'; $out[] = '</em>';
		$in[] = '_em '; $out[] = '<em>';
		$in[] = ' em_'; $out[] = '</em>';
		//$in[] = '_code '; $out[] = '<pre>';
		//$in[] = ' code_'; $out[] = '</pre>';
		/*$in[] = '-->'; $out[] = '<center>';
		$in[] = '<--'; $out[] = '</center>';
		$in[] = '-->'; $out[] = '<right>';
		$in[] = '-->'; $out[] = '</right>';
		$in[] = '<--'; $out[] = '<left>';
		$in[] = '<--'; $out[] = '</left>';
		$in[] = '<--'; $out[] = '<justify>';
		$in[] = '-->'; $out[] = '</justify>';
		$in[] = '>['; $out[] = '<div style="float: right;">';
		$in[] = ']>'; $out[] = '</div>';
		$in[] = '<['; $out[] = '<div style="float: left;">';
		$in[] = ']<'; $out[] = '</div>';*/

		$text = str_replace($in,$out,$text);
		$text = preg_replace_callback('!_code(.*)code_!Usi',create_function('$value','return \'<pre>\'.str_replace(\' \',\'&nbsp;\',$value[1]).\'</pre>\';'),$text);
		$text = preg_replace('!->(.*)<-!Usi','<center>$1</center>',$text);
		$text = preg_replace('!->(.*)->!Usi','<right>$1</right>',$text);
		$text = preg_replace('!<-(.*)<-!Usi','<left>$1</left>',$text);
		$text = preg_replace('!<-(.*)->!Usi','<justify>$1</justify>',$text);
		$text = preg_replace('!>\[(.*)\]>!Usi','<div style="float: right;">$1</div>',$text);
		$text = preg_replace('!<\[(.*)\]<!Usi','<div style="float: left;">$1</div>',$text);
		
		
		$text = preg_replace_callback('!{{(.*(\w+)(\.\w*)*)}}!Usi','convertImg',$text);
		
		$text = preg_replace('!  (\w)!Usi','<p>$1',$text);
		$text = preg_replace('!  \[\[\[([^\n]*)\]\]\]\r\n!Usi','<h4>$1</h4>',$text);
		$text = preg_replace('!  \[\[([^\n]*)\]\]\r\n!Usi','<h3>$1</h3>',$text);
		$text = preg_replace('!  \[([^\n]*)\]\r\n!Usi','<h2>$1</h2>',$text);
		$text = nl2br($text);
		return $text;
	}
}

function convertImg($arg) {
	//On récupère le nom du fichier miniature
	$miniature = preg_replace('!\.(\w*)$!','.miniature.$1',$arg[1]);
	if(file_exists($miniature))
		return '<a href="'.$arg[1].'"><img src="'.$miniature.'" alt ="'.$arg[2].'" /></a>';
	else
		return '<img src="'.$arg[1].'" alt ="'.$arg[2].'" />';
}
?>